package com.android.baseconfig.common.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import androidx.core.app.ActivityCompat;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: GetAndroidUniqueMark <br>
 * 作者: lei <br>
 * 日期: 2018/3/17 <br>
 */
public class GetAndroidUniqueMark {

    @SuppressLint("HardwareIds")
    public static String getUniqueId(Context context) {
        // ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数，当设备被wipe后该数重置。
                String androidId = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        UUID deviceUuid = new UUID(androidId.hashCode(), androidId.hashCode());
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (ActivityCompat.checkSelfPermission(context,
                        Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                    TelephonyManager tm =
                            (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    if (Build.VERSION.SDK_INT < 29) {
                        String deviceId = tm.getImei();
                        String simSerialNum = tm.getSimSerialNumber();
                        deviceUuid = new UUID(androidId.hashCode(),
                                ((long)deviceId.hashCode() << 32) | simSerialNum.hashCode());
                        Logc.d("androidId: " + androidId + ", deviceId: " + deviceId);
                    }
                }
            } else {
                TelephonyManager tm =
                        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                String deviceId = tm.getDeviceId();
                String simSerialNum = tm.getSimSerialNumber();
                deviceUuid = new UUID(androidId.hashCode(),
                        ((long)deviceId.hashCode() << 32) | simSerialNum.hashCode());
                Logc.d("androidId: " + androidId + ", deviceId: " + deviceId);
            }
            return deviceUuid.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return deviceUuid.toString();
        }
    }

    public static String toMD5(String text) throws NoSuchAlgorithmException {
        //获取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通过摘要器对字符串的二进制字节数组进行hash计算
        byte[] digest = messageDigest.digest(text.getBytes());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            //循环每个字符 将计算结果转化为正整数;
            int digestInt = digest[i] & 0xff;
            //将10进制转化为较短的16进制
            String hexString = Integer.toHexString(digestInt);
            //转化结果如果是个位数会省略0,因此判断并补0
            if (hexString.length() < 2) {
                sb.append(0);
            }
            //将循环结果添加到缓冲区
            sb.append(hexString);
        }
        //返回整个结果
        return sb.toString().substring(8, 24);
    }
}

